Obsidian マンスリービュー タスク + 行間メモ
2024-10-15
ノートがない日の日付をクリックしたときのノート作成先フォルダをルートフォルダから"notes"フォルダに修正
code:js
`dataviewjs
// 時間と分のフォーマット関数
function formatTime(minutes) {
let hours = Math.floor(minutes / 60);
let remainingMinutes = minutes % 60;
if (hours > 0) {
return ${hours}h${remainingMinutes}m;
} else {
return ${remainingMinutes}m;
}
}
// 曜日を日本語の一文字に変換する関数
function getJapaneseDayOfWeek(day) {
const daysInJapanese = "日", "月", "火", "水", "木", "金", "土";
return daysInJapaneseday;
}
// フィルターキーワードを取得するテキストボックスを作成
let filterInput = document.createElement('input');
filterInput.type = 'text';
filterInput.placeholder = 'フィルターキーワードを入力';
filterInput.style.marginBottom = '10px';
filterInput.style.width = '100%';
// "フィルタ" ボタンを作成
let filterButton = document.createElement('button');
filterButton.innerText = 'フィルタ';
filterButton.style.display = 'block';
filterButton.style.marginBottom = '20px';
// テキストボックスとボタンを最初に表示
dv.container.appendChild(filterInput);
dv.container.appendChild(filterButton);
// ボタンがクリックされたときのイベントリスナー
filterButton.addEventListener('click', async () => {
let filterKeyword = filterInput.value; // テキストボックスからキーワードを取得
let filterEnabled = filterKeyword.length > 0;
// 再検索の際に以前のコンテンツをクリア
dv.container.innerHTML = "";
// 検索UIを再度表示
dv.container.appendChild(filterInput);
dv.container.appendChild(filterButton);
// 今日の日付を取得
let today = window.moment().startOf('day');
// 取得するデータの期間をカレンダーで表示している期間に設定
let startDate = today.clone().startOf('month');
let endDate = today.clone().endOf('month');
// カレンダーの行ごとの日付を格納する配列
let calendar = [];
// 月初めの日から月末の日までループ
for (let currentDate = startDate.clone(); currentDate.isBefore(endDate); currentDate.add(1, 'days')) {
let filePath = notes/${currentDate.format('YYYY-MM-DD')}.md;
let tasksForDay = [];
let totalMinutes = 0;
// ファイルが存在するか確認
if (await app.vault.adapter.exists(filePath)) {
let fileContent = await dv.io.load(filePath);
let lines = fileContent.split('\n');
// 各行を確認してタスクを抽出
lines.forEach(line => {
let match = line.match(/(\d{2}:\d{2})(-\d{2}:\d{2})?\s*(\d+\\)?\s*(.*)/);
if (match) {
let startTime = match1 || "-";
let minutes = null;
if (match3) {
minutes = parseInt(match3, 10);
}
let task = match4 || "";
task = task.replace(/\[(^\]+)\]\(^\)+\)/g, "$1");
if (!filterEnabled || task.includes(filterKeyword)) {
let timeDisplay = minutes !== null ? (${formatTime(minutes)}) : "";
tasksForDay.push(・${startTime} ${task}${timeDisplay});
if (minutes !== null) {
totalMinutes += minutes;
}
}
}
});
// カレンダーの日付にタスクを追加
calendar.push({
date: currentDate.format('D'),
fileName: ${currentDate.format('YYYY-MM-DD')}, // デイリーノートのファイル名
dayOfWeek: currentDate.day(),
tasks: tasksForDay.join('<br>'),
total: formatTime(totalMinutes)
});
} else {
calendar.push({
date: currentDate.format('D'),
fileName: notes/${currentDate.format('YYYY-MM-DD')},
dayOfWeek: currentDate.day(),
tasks: '',
total: ''
});
}
}
// カレンダーの曜日の行を作成
let calendarHTML = "<table style='width: 100%; text-align: center; table-layout: fixed; border-spacing: 0;'><tr>";
const daysInJapanese = "日", "月", "火", "水", "木", "金", "土";
daysInJapanese.forEach(day => {
calendarHTML += <th style="text-align: center; color: gray;">${day}</th>;
});
calendarHTML += "</tr><tr>";
// 1日目の前に空のセルを追加(週の開始日を調整)
let firstDay = calendar0.dayOfWeek;
for (let i = 0; i < firstDay; i++) {
calendarHTML += "<td></td>";
}
// 日付リンクを作成してカレンダーに表示
calendar.forEach((day, index) => {
if (index !== 0 && day.dayOfWeek === 0) {
// 日曜日(週の初め)ごとに新しい行を開始
calendarHTML += "</tr><tr>";
}
// 各セルの集計結果をタスクリストの上に表示し、タスクリストには・を付けて小さくする
calendarHTML += <td><a href="#" class="day-link" data-file="${day.fileName}";" style="color: gray;"><b>${day.date}</b></a><b>${day.total}</b><br><div style="font-size: small; line-height: 1.2;">${day.tasks}</div></td>;
// calendarHTML += <td><a href="#" class="day-link" data-file="${day.fileName}" style="color: gray;"><b>${day.date}</b></a><b style="margin-left: 5px;">${day.total}</b><br><div style="font-size: small; line-height: 1.2; text-align: left;">${day.tasks}</div></td>;
});
calendarHTML += "</tr></table>";
// カレンダーを表示
dv.container.innerHTML += calendarHTML;
// すべてのリンクにクリックイベントを追加して、その日のノートを開く
document.querySelectorAll('.day-link').forEach(link => {
link.addEventListener('click', (event) => {
event.preventDefault();
let fileName = link.getAttribute('data-file');
app.workspace.openLinkText(fileName, '', true);
});
});
});
`